package com.cskaoyan.controller.admin.promotecontroller;

import com.cskaoyan.bean.goods.Goods;


import com.cskaoyan.bo.promoteBO.GrouponrulesBO;
import com.cskaoyan.mapper.goodsmapper.GoodsMapper;

import com.cskaoyan.service.promoteservice.GrouponService;
import com.cskaoyan.vo.BaseRespVo;

import com.cskaoyan.vo.promotevo.Groupon_rulesVO;
import com.cskaoyan.vo.promotevo.GrouponrulesCreateVO;

import com.cskaoyan.vo.promotevo.ListuserVO;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

/**
 * @Author: feisl
 * @Date: 2021/5/10 1:19
 */
@RestController
@RequestMapping("admin/groupon")
public class GrouponController {
    @Autowired
    GrouponService grouponService;
    @RequiresPermissions("admin:groupon:list")
    @RequestMapping("list")
    public BaseRespVo list(Integer page, Integer limit, Integer goodsId, String sort, String order) {
        Groupon_rulesVO groupon_rulesVO = grouponService.queryGrouponRulesAll(page, limit, goodsId, sort, order);

        return BaseRespVo.ok(groupon_rulesVO);

    }

    @Autowired
    GoodsMapper goodsMapper;
    @RequiresPermissions("admin:groupon:create")
    @RequestMapping("create")
    public BaseRespVo create(@RequestBody Map map) throws ParseException {
        try {
            Integer goodsId = Integer.parseInt((String) map.get("goodsId"));
        } catch (Exception e) {
            return BaseRespVo.fail402("商品id参数异常");
        }
        try {
            Integer discountMember = Integer.parseInt((String) map.get("discountMember"));
        } catch (Exception e) {
            return BaseRespVo.fail402("团购人数参数异常");
        }
        try {
            String time = (String) map.get("expireTime");
        } catch (Exception e) {
            return BaseRespVo.fail402("过期时间参数异常");
        }
//        try {
//            BigDecimal discount = (BigDecimal) map.get("discount");
//        }catch (Exception e){
//            return BaseRespVo.fail402("团购折扣参数异常");
//        }
        Integer goodsId = Integer.parseInt((String) map.get("goodsId"));
        String object = (String) map.get("discount");
        char[] chars = object.toCharArray();
        BigDecimal discount = new BigDecimal(chars, 0, chars.length);
        Integer discountMember = Integer.parseInt((String) map.get("discountMember"));
        if (discountMember < 0) return BaseRespVo.fail402("团购人数异常");
        String expireTime = (String) map.get("expireTime");
        GrouponrulesBO grouponrulesBO = new GrouponrulesBO(goodsId, discount, discountMember, expireTime);

//        判断商品是否存在
        int id = grouponrulesBO.getGoodsId();
        Goods goods = goodsMapper.selectByPrimaryKey(id);

        if(goods == null){
            return BaseRespVo.fail402("参数值不对");
        }
        Date date = new Date();
//        SecurityUtils.getSubject().getSession().setAttribute("username","user");
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date time = simpleDateFormat.parse(grouponrulesBO.getExpireTime() );
        if(time.compareTo(date)<0) {
            return BaseRespVo.fail402("过期时间设定不对");
        }
        if (goods == null) {
            return BaseRespVo.fail402("商品不存在");

        }


        GrouponrulesCreateVO grouponrulesCreateVO = grouponService.insert(grouponrulesBO);

        return BaseRespVo.ok(grouponrulesCreateVO);

    }

    @RequiresPermissions("admin:groupon:update")
    @RequestMapping("update")
    public BaseRespVo update(@RequestBody GrouponrulesBO grouponrulesBO) throws ParseException {
//        判断商品是否存在
        int id = grouponrulesBO.getGoodsId();
        Goods goods = goodsMapper.selectByPrimaryKey(id);
        if (goods == null) {
            return BaseRespVo.fail402("商品不存在");
        }
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat();
        grouponrulesBO.setUpdateTime(new Date());

        grouponService.update(grouponrulesBO);

        return BaseRespVo.ok();
    }

    @RequiresPermissions("admin:groupon:read")
    @RequestMapping("listRecord")
    public BaseRespVo listRecord() {

        ListuserVO listuserVO = new ListuserVO();
        return BaseRespVo.ok(listuserVO);
    }
    @RequiresPermissions("admin:groupon:delete")
    @RequestMapping("delete")
    public BaseRespVo delete(@RequestBody Map map) {

        Object id = map.get("id");

        grouponService.delete((Integer) id);

        return BaseRespVo.ok();
    }
}
